package main

import (
	"log"
	global2 "longmen/server/config/global"
	sendticket "longmen/server/internal/rpc/scipts/send_ticket"
	"longmen/server/pkg/common/services"
	"longmen/server/pkg/grpc_client"
	"os"
	"strings"
	"time"
)

func main() {
	global2.AppName = "webapi_rpc_script"
	global2.Initialize("../../../config/app.properties")

	// 获取命令行参数
	if len(os.Args) < 2 {
		log.Fatalf("缺少命令参数，如: provideTicket")
	}
	action := strings.ToLower(os.Args[1])

	service := &services.Service{global2.GetX()}
	service.InitLiveCallBackServer()
	s := &sendticket.Script{
		Service: service,
	}

	grpc_client.InitGrpc()

	// 根据命令调用方法
	switch action {
	case "provideticket":
		s.ProvideTicket()
	case "check6t":
		var startTime, endTime time.Time
		const layout = "2006-01-02"

		if len(os.Args) >= 4 {
			var err error
			startTime, err = time.ParseInLocation(layout, os.Args[2], time.Local)
			if err != nil {
				log.Fatalf("解析 startTime 失败: %v", err)
			}
			endTime, err = time.ParseInLocation(layout, os.Args[3], time.Local)
			if err != nil {
				log.Fatalf("解析 endTime 失败: %v", err)
			}
		} else {
			// 默认使用前一天的 00:00 到 23:59:59
			yesterday := time.Now().AddDate(0, 0, -1)
			startTime = time.Date(yesterday.Year(), yesterday.Month(), yesterday.Day(), 0, 0, 0, 0, time.Local)
			endTime = time.Date(yesterday.Year(), yesterday.Month(), yesterday.Day(), 23, 59, 59, 0, time.Local)
		}

		s.GetBindUsers(startTime, endTime)
	default:
		log.Fatalf("未知操作命令: %s", action)
	}
}
